package com.leetcode;

import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 215. 数组中的第K个最大元素
 */
public class Solution215_1 {

    /**
     * 使用最大堆
     *
     * @param nums
     * @param k
     * @return
     */
    public int findKthLargest(int[] nums, int k) {
        Queue<Integer> queue = new PriorityQueue<>((a, b) -> b - a);
        for (int i = 0; i < nums.length; i++) {
            queue.offer(nums[i]);
        }
        for (int i = 1; i < k; i++) {
            queue.poll();
        }
        return queue.poll();
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3, 2, 1, 5, 6, 4};
//        int[] nums = new int[]{3};
        int k = 5;
        int num = new Solution215_2().findKthLargest(nums, k);
        System.out.println(num);
    }
}
